<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <style type="text/css">
@import "<%=request.getContextPath() %>/js/dijit/themes/soria/soria.css";
@import "<%=request.getContextPath() %>/js/dijit/themes/tundra/tundra.css";
@import "<%=request.getContextPath() %>/js/dojox/grid/resources/Grid.css";
@import "<%=request.getContextPath() %>/js/dojox/grid/resources/tundraGrid.css";
@import "<%=request.getContextPath() %>/js/dojox/grid/enhanced/resources/tundraEnhancedGrid.css";
@import "<%=request.getContextPath() %>/js/dojox/grid/enhanced/resources/EnhancedGrid_rtl.css";
.dojoxGrid table { margin: 0; } html, body { width: 100%; height: 100%;
            margin: 0; }
</style>
<script type="text/javascript">
dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.parser");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.DateTextBox");
dojo.require("dijit.form.ComboBox");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.TabContainer");
dojo.require("dojox.data.QueryReadStore");
dojo.require("dijit.TitlePane");
dojo.require("dijit.Toolbar");
dojo.require("dojox.grid.EnhancedGrid");
dojo.require("dojox.grid.enhanced.plugins.DnD");
dojo.require("dojox.grid.enhanced.plugins.Menu");
dojo.require("dojox.grid.enhanced.plugins.NestedSorting");
dojo.require("dojox.grid.enhanced.plugins.IndirectSelection");
dojo.require("dojo.date");
dojo.require("dojo.date.locale");

var layout = [{
                    field: 'Auction',
                    name: 'Auction',
                    width: '200px'
                },               
                {
                    field: 'item',
                    name: 'Item',
                    width: 'auto'
                },
                {
                    field: 'amount',
                    name: 'Amount',
                    width: 'auto'
                },
                {            
                    field: 'endDate',
                    name: 'EndDate',
                    width: 'auto'                
                }];

</script>

<script>
	function cancelBid() {

                    var items = grid.selection.getSelected();
                    var selectedItems = new Array();
					var value = 0;
                    if (items.length) {
                        // Iterate through the list of selected items.
                        // The current item is available in the variable
                        // "selectedItem" within the following function:
                        dojo.forEach(items, function(selectedItem) {
                            if (selectedItem !== null) {
                                // Iterate through the list of attributes of each item.
                                // The current attribute is available in the variable
                                // "attribute" within the following function:
                                dojo.forEach(grid.store.getAttributes(selectedItem), function(attribute) {
                                    // Get the value of the current attribute:
                                    if (attribute == "bidId") {
                                    	value = grid.store.getValues(selectedItem, attribute);
                                    	selectedItems.push(value);
                                    }else if(attribute == "endDate"){  
                                    	var today = new Date();      
                                    	//alert("today = "+today);                          	
                                    	var endDate = grid.store.getValues(selectedItem, attribute);
                                    	//alert("endDate b4 parsing = "+endDate);
                                    	endDate = dojo.date.locale.parse(endDate,{datePattern: "yyyy-MM-dd", selector: "date"});                                    	
                                    	//alert("endDate = "+endDate);
                                    	var diff = dojo.date.difference(today,endDate);
                                    	alert("diff = "+diff);
                                    	if(diff < 0){
                                    		//alert("end date < today");
                                    		alert("Can not cancel Bid. Auction has expired now.");
                                    		return;
                                    	}                                   	
                                    }
                                }); // end forEach
                            } // end if
                        }); // end forEach
                    } // end if

                 	var xhrArgs = {
                        url: "cancelPlacedBids.htm",
                        mimetype: "text/json",
                        content: {selectedIds: selectedItems},
                        load: function(responseData) {
                           var bidData = dojo.fromJson(responseData);
                           //dojo.byId("response").innerHTML = "Cancelled";
                           try{
                               var newStore = new dojo.data.ItemFileReadStore({data: bidData});
                               gridMyBids.setStore(newStore);
                           }catch(e){
                                 console.log(e);
                           }
                        },
                        error: function(error) {
                            //We'll 404 in the demo, but that's okay.  We don't have a 'postIt' service on the
                            //docs server.
                            dojo.byId("response").innerHTML = "Try Again. Unable to approve.";
                            alert("Try Again. Unable to approve.")
                        }
                    }
                    //dojo.byId("response").innerHTML = "Cancelling bids....."
                    //Call the asynchronous xhrPost
                     var deferred = dojo.xhrPost(xhrArgs);

	}
	
</script>

<div dojoType="dijit.Toolbar">
     <div dojoType="dijit.form.Button" jsId="btnCancelBid" onClick="cancelBid()">
    	<span>Cancel Bid</span>
    </div>
</div>
<br/>
<div dojoType="dijit.TitlePane" title="<b>Bids</b>" style="width:90%;height:80%;" class="tundra">
	<!-- Data stores read data from a data store such as a servlet -->
	<div dojoType="dojox.data.QueryReadStore" jsId="myBidsStore" 	url="listMyBids.htm">
    </div>

	<!-- A grid displays data and offer useful operations such as client sorting -->
	<div id="gridMyBids" jsId="grid" dojoType="dojox.grid.EnhancedGrid" structure="layout"
	store="myBidsStore" style="width:90%;height:80%;"
	plugins="{nestedSorting: true, dnd: true, indirectSelection: true}" rowSelector="0px">
    </div>
</div>
<div id="response"></div>



